#
import pandas as pd
df = pd.read_csv('data/learn_pandas.csv')
# 依据 性别 分组，统计全国人口 寿命 的 平均值
df.groupby('Gender')['Longevity'].mean()
#性别统计身高中位数
df.groupby('Gender')['Height'].median()


# 多维度分组
df.groupby(['School', 'Gender'])['Height'].mean()
condition = df.Weight > df.Weight.mean()
df.groupby(condition)['Height'].mean()
df[['School', 'Gender']].drop_duplicates()
df.groupby([df['School'], df['Gender']])['Height'].mean()

gb = df.groupby(['School', 'Grade'])
res = gb.groups

gb.get_group(('Fudan University', 'Freshman')).iloc[:3, :3]

gb = df.groupby('Gender')['Height']

gb.agg(['sum', 'idxmax', 'skew'])
gb.agg({'Height':['mean','max'], 'Weight':'count'})

gb.agg(lambda x: x.max()-x.min())

def my_func(s):
    res = 'High'
    if s.mean() <= df[s.name].mean():
        res = 'Low'
    return res


gb.agg(my_func)

gb.agg([('range', lambda x: x.max()-x.min()), ('my_sum', 'sum')])

gb.filter(lambda x: x.shape[0] > 100).head()
#https://blog.csdn.net/qq_40587575/article/details/81204514
#https://blog.csdn.net/qq_40587575/article/details/81204514
#https://blog.csdn.net/qq_40587575/article/details/81204514